package edu.upb.acs.jobsmaps.activity.details;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import edu.upb.acs.jobsmaps.AddressLookup;
import edu.upb.acs.jobsmaps.R;
import edu.upb.acs.jobsmaps.database.task.GetCompanyAfterIdTask;
import edu.upb.acs.jobsmaps.entity.Company;

public class ViewCompanyDetailsActivity extends Activity {
	private static final String TAG = ViewCompanyDetailsActivity.class.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.company_details);

		Log.d(TAG, "Initialize activity");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "Start activity");

		Bundle extras = getIntent().getExtras();
		String id = "";
		String ratingAdded = "";
		if (extras != null) {
			id = extras.getString("company_id");
			ratingAdded = extras.getString("rating");
			Log.d(TAG, "Show details for company with id = " + id);
		} else {
			Log.e(TAG, "No company id received");
			throw new RuntimeException("Called ViewCompanyDetailsActivity without job id");
		}

		Company company = null;
		GetCompanyAfterIdTask getCompanyAfterIdTask = new GetCompanyAfterIdTask();
		try {
			company = getCompanyAfterIdTask.execute(id).get();
			Log.d(TAG, "Got company = " + company);
		} catch (Exception e) {
			Log.e(TAG, "Can not get company", e);
		}

		TextView companyName = (TextView) findViewById(R.id.companyName);
		companyName.setText(company.getName());

		AddressLookup addressLookup = new AddressLookup(company.getLatitude(), company.getLongitude(), getApplicationContext());
		TextView jobAddress = (TextView) findViewById(R.id.companyAddress);
		jobAddress.setText(addressLookup.getAddress());

		TextView companyProfile = (TextView) findViewById(R.id.companyProfile);
		companyProfile.setText(company.getProfile());

		if (null != company.getDescription()) {
			TextView jobDescription = (TextView) findViewById(R.id.companyDescription);
			StringBuilder description = new StringBuilder(company.getDescription());

			jobDescription.setText(Html.fromHtml(description.toString()));
		}

		if (null != company.getAverageRating()) {
			TextView ratingText = (TextView) findViewById(R.id.companyRating);
			Double averageRating = company.getAverageRating();
			String format = String.format("%.2f", averageRating);
			if (ratingAdded != null) {
				String source = "Rating was added. New rating: " + format;
				ratingText.setText(source);
			} else {
				String source = "Rating: " + format;
				ratingText.setText(source);
			}
		}

		final Company finalCompany = company;
		Button button = (Button) findViewById(R.id.companyMarkerViewCompanyDetails);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent internetIntent = new Intent(Intent.ACTION_VIEW);
				internetIntent.setDataAndType(Uri.parse(finalCompany.getCompanyUrl()), "text/html");
				internetIntent.addCategory(Intent.CATEGORY_BROWSABLE);
				startActivity(internetIntent);
			}
		});

		Button buttonRate = (Button) findViewById(R.id.rateCompany);
		buttonRate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent i = new Intent(ViewCompanyDetailsActivity.this,
						RatingActivity.class);
				i.putExtra("id", finalCompany.getId());
				startActivity(i);
			}
		});


	}


}
